package com.example.demo.service.impl;

import com.example.demo.dao.UserDao;
import com.example.demo.dao.impl.UserDaoImpl;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
//@Component//将当前类交给IOC容器管理
@Component
public class UserServiceImpl implements UserService {
    //创建UserDao对象
    @Autowired//通过IOC容器获取UserDao对象,代替new UserDaoImpl()
    private UserDao userDao;
    @Override
    public List<User> findAll() {
        //调用dao层查询所有用户信息
        List<String> lines=userDao.findAll();
        //解析用户信息
        List<User> userList=lines.stream().map(line -> {
            String[] parts=line.split(",");
            Integer id=Integer.parseInt(parts[0]);
            String username=parts[1];
            String password=parts[2];
            String name=parts[3];
            Integer age=Integer.parseInt(parts[4]);
            LocalDateTime updateTime=LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new User(id,username,password,name,age,updateTime);
        }).toList();//转为List集合
        return userList;
    }
}
